<template>
  <div class="dashboard-editor-container">
    <el-row style="background: #fff; padding: 16px 16px 0; margin-bottom: 32px">
      <line-chart :chart-data="lineChartData" :xData="xData" />
    </el-row>
  </div>
</template>

<script>
import LineChart from "../../dashboard/LineChart2";
import { electionCount } from "@/api/register/appoint.js";
const lineChartData = {
  newVisitis: {
    data1: [],
    data2: [],
    data3: [],
    data4: [],
  },
};

export default {
  name: "Index",
  components: {
    LineChart,
  },
  data() {
    return {
      lineChartData: lineChartData.newVisitis,
      xData: [],
      xDataSet: [], // 唯一一个sessionId
    };
  },
  methods: {
    handleSetLineChartData(type) {
      this.lineChartData = lineChartData[type];
    },
  },
  mounted() {
    electionCount().then((res) => {
      this.lineChartData = {
        data1: [],
        data2: [],
        data3: [],
        data4: [],
      };
      this.xDataSet = [];
      res.rows.map((item) => {
        // 1、先确定x轴
        if (!this.xDataSet.includes(item.electionId)) {
          this.xDataSet.push(item.electionId);
          this.xData.push(item.sessionName || "无");
        }
      });
      // 2、再确定y轴
      this.xDataSet.forEach((electionId) => {
        let filterY = res.rows.filter((item) => item.electionId === electionId);
        let filterYIds = filterY
          .map((item) => item.electType)

          [(1, 2, 3, 4)].forEach((i) => {
            // 包含了i，意味著 有 num值
            if (filterYIds.includes(i)) {
              let y = filterY.filter((item) => item.electType == i);
              switch (i) {
                case 1:
                  this.lineChartData.data1.push(y[0].num);
                  break;
                case 2:
                  this.lineChartData.data2.push(y[0].num);
                  break;
                case 3:
                  this.lineChartData.data3.push(y[0].num);
                  break;
                case 4:
                  this.lineChartData.data4.push(y[0].num);
                  break;
                default:
                  break;
              }
            } else {
              switch (i) {
                case 1:
                  this.lineChartData.data1.push(0);
                  break;
                case 2:
                  this.lineChartData.data2.push(0);
                  break;
                case 3:
                  this.lineChartData.data3.push(0);
                  break;
                case 4:
                  this.lineChartData.data4.push(0);
                  break;
                default:
                  break;
              }
            }
          });
      });
    });
  },
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  //   background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
